"""
商谈记录模型
"""
from datetime import datetime
from app.extensions import db


class Negotiation(db.Model):
    """合作商谈记录表"""
    id = db.Column(db.Integer, primary_key=True)
    customer_name = db.Column(db.String(100), nullable=False, comment='客户姓名')
    project_number = db.Column(db.String(50), comment='项目序号')
    category = db.Column(db.String(20), nullable=False, comment='类别：线上合作商谈/线下合作商谈')
    is_cross_project = db.Column(db.Boolean, default=False, comment='是否跨项目合作')
    staff_names = db.Column(db.Text, comment='项目负责人姓名（JSON格式）')
    project_name = db.Column(db.String(100), comment='所属项目')
    start_time = db.Column(db.DateTime, nullable=False, comment='开始时间')
    end_time = db.Column(db.DateTime, nullable=False, comment='结束时间')
    duration_minutes = db.Column(db.Integer, comment='时长（分钟）')
    notes = db.Column(db.Text, comment='备注信息')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 为兼容性添加的字段
    customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'), comment='客户ID')
    staff_id = db.Column(db.Integer, db.ForeignKey('staff.id'), comment='员工ID')
    negotiation_date = db.Column(db.Date, comment='商谈日期')
    duration = db.Column(db.Integer, comment='时长（分钟）')
    content = db.Column(db.Text, comment='商谈内容')
    result = db.Column(db.Text, comment='商谈结果')
    
    # 关系
    customer = db.relationship('Customer', backref=db.backref('negotiations', lazy=True))
    staff = db.relationship('Staff', backref=db.backref('negotiations', lazy=True))
    
    def __repr__(self):
        return f'<Negotiation {self.customer_name}-{self.category}>'


class UploadException(db.Model):
    """上传异常数据表"""
    id = db.Column(db.Integer, primary_key=True)
    filename = db.Column(db.String(255), nullable=False, comment='上传文件名')
    status = db.Column(db.String(20), default='待处理', comment='处理状态')
    uploader = db.Column(db.String(100), comment='上传文件人')
    processor = db.Column(db.String(100), comment='处理人')
    upload_time = db.Column(db.DateTime, default=datetime.utcnow, comment='上传时间')
    process_time = db.Column(db.DateTime, comment='处理时间')
    error_data = db.Column(db.Text, comment='异常数据（JSON格式）')
    error_reason = db.Column(db.Text, comment='异常原因')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 为兼容性添加的字段
    row_number = db.Column(db.Integer, comment='行号')
    error_message = db.Column(db.Text, comment='错误信息')
    raw_data = db.Column(db.Text, comment='原始数据')
    is_processed = db.Column(db.Boolean, default=False, comment='是否已处理')
    processed_at = db.Column(db.DateTime, comment='处理时间')
    
    def __repr__(self):
        return f'<UploadException {self.filename}-{self.status}>'
